﻿using IoTClient.Clients.PLC;
using IoTClient.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QSiemensPlcPlayer
{
	/// <summary>
	/// 三菱plc
	/// </summary>
	public class QMitsubishiPlc
	{

		private string ip { get; set; }
		private int port { get; set; }
		private MitsubishiVersion mitsubishiVersion { get; set; }

		private MitsubishiClient m_plc;

		/// <summary>
		/// MitsubishiClient client = new MitsubishiClient(MitsubishiVersion.Qna_3E, "127.0.0.1", 6000);
		/// </summary>
		/// <param name="ip"></param>
		/// <param name="port"></param>
		/// <param name="version"></param>
		public QMitsubishiPlc(string ip, int port, MitsubishiVersion version)
		{
			this.ip = ip;
			this.port = port;

			this.mitsubishiVersion = version;

			m_plc = new MitsubishiClient(mitsubishiVersion, ip, port);
		}

		public MitsubishiClient GetPlc()
		{
			if (m_plc == null) m_plc = new MitsubishiClient(mitsubishiVersion, ip, port);
			if (!m_plc.Connected) m_plc.Open();
			return m_plc;
		}



		#region 三菱plc读操作
		//温度探头1	07D0	读
		//温度探头2	07D1	读
		//温度探头3	07D2	读
		//室外温度探头	07D3	读
		//二氧化碳探头	07D4	读

		public int 温度探头1()
		{
			var plc = GetPlc();
			var res = plc.ReadInt32(QMitsubishiPlcAddress.温度探头1).Value;
			return res;

		}
		public int 温度探头2()
		{
			var plc = GetPlc();
			var res = plc.ReadInt32(QMitsubishiPlcAddress.温度探头2).Value;
			return res;

		}
		public int 温度探头3()
		{
			var plc = GetPlc();
			var res = plc.ReadInt32(QMitsubishiPlcAddress.温度探头3).Value;
			return res;

		}
		public int 室外温度探头()
		{
			var plc = GetPlc();
			var res = plc.ReadInt32(QMitsubishiPlcAddress.室外温度探头).Value;
			return res;

		}
		public int 二氧化碳探头()
		{
			var plc = GetPlc();
			var res = plc.ReadInt32(QMitsubishiPlcAddress.二氧化碳探头).Value;
			return res;

		}

		#endregion


		#region 三菱plc写操作
		/// <summary>
		/// 模式控制
		/// 写入1对应手动模式写入
		/// 2对应冬季模式写入
		/// 3对应春秋季模式写入
		/// 4对应夏季模式
		/// </summary>
		/// <param name="model">写入1对应手动模式写入2对应冬季模式写入3对应春秋季模式写入4对应夏季模式</param>
		public void ModeControl(int model)
		{
			var plc = GetPlc();
			plc.Write(QMitsubishiPlcAddress.模式控制, model);
		}
		public void 大风机1组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机1组开关, value);
		}
		public void 大风机2组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机2组开关, value);
		}
		public void 大风机3组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机3组开关, value);
		}
		public void 大风机4组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机4组开关, value);
		}
		public void 大风机5组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机5组开关, value);
		}


		public void 侧排风1组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风1组开关, value);
		}
		public void 侧排风2组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风2组开关, value);
		}
		public void 侧排风3组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风3组开关, value);
		}
		public void 侧排风4组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风4组开关, value);
		}
		public void 侧排风5组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风5组开关, value);
		}
		public void 侧排风6组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风6组开关, value);
		}
		public void 新风开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.新风开关, value);
		}

		/// <summary>
		/// 写入1开启50%，写入2开100%，写入0关闭
		/// </summary>
		/// <param name="open"></param>
		public void 电动百叶窗(int value)
		{
			var plc = GetPlc();

			plc.Write(QMitsubishiPlcAddress.电动百叶窗, value);
		}
		public void 水泵(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.水泵, value);
		}

		/// <summary>
		/// 写入1开启30%，写入2开600%，写入3开启100%，写入0关闭
		/// </summary>
		/// <param name="open"></param>
		public void 导流板(int value)
		{
			var plc = GetPlc();

			plc.Write(QMitsubishiPlcAddress.导流板, value);
		}

		/// <summary>
		/// 写入0是PLC控制，写入1是PC控制
		/// </summary>
		/// <param name="open"></param>
		public void PC与PLC切换(int value)
		{
			var plc = GetPlc();

			plc.Write(QMitsubishiPlcAddress.PC与PLC切换, value);
		}










		#endregion



	}
}
